# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : lHotkey.py
# Date      : 2018/8/25
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------


__author__ = "lin lingwei"

import maya.cmds as mc
import maya.mel as mel
import scripts.commonCmds as con

def setup():
    # Set hotkeySet
    ver = mc.about(version=True)
    if int(ver) > 2015:
        hotkeySetName = 'hotkeySet_lc'
        # currentSet = mc.hotkeySet(q=True,current=True)
        if not mc.hotkeySet(hotkeySetName, exists=True):
            mc.hotkeySet(hotkeySetName, current=True)
        else:
            mc.hotkeySet(hotkeySetName, edit=True, current=True)
    
    # Single display selection
    cmdName = 'showSelect'
    ann = "[ Alt + q ]  Single display selection"
    command = 'python(\"lHotkey.hotkeyCmd(\\"%s\\")\")' % cmdName
    mc.nameCommand(cmdName, annotation=ann, c=command)
    mc.hotkey(keyShortcut='q', altModifier=True, ctrlModifier=False, name=cmdName)
    
    # Reset the choice of channel box attribute
    cmdName = 'resetChannelBoxAttribute'
    ann = "[ Shift + Alt + r ]  Reset the choice of channel box attribute"
    command = 'python(\"lHotkey.hotkeyCmd(\\"%s\\")\")' % cmdName
    mc.nameCommand(cmdName, annotation=ann, c=command)
    mc.hotkey(keyShortcut='R', altModifier=True, ctrlModifier=False, name=cmdName)
    
    # Reset the choice of translate attribute
    cmdName = 'resetTranslate'
    ann = "[ Shift + Alt + w ]  Reset the choice of translate attribute"
    command = 'python(\"lHotkey.hotkeyCmd(\\"%s\\")\")' % cmdName
    mc.nameCommand(cmdName, annotation=ann, c=command)
    mc.hotkey(keyShortcut='W', altModifier=True, ctrlModifier=False, name=cmdName)
    
    # Reset the choice of rotate attribute
    cmdName = 'resetRotate'
    ann = "[ Shift + Alt + e ]  Reset the choice of rotate attribute"
    command = 'python(\"lHotkey.hotkeyCmd(\\"%s\\")\")' % cmdName
    mc.nameCommand(cmdName, annotation=ann, c=command)
    mc.hotkey(keyShortcut='E', altModifier=True, ctrlModifier=False, name=cmdName)
    
    # X-Ray
    cmdName = 'xRay'
    ann = "[ Shift + Alt + x ]  X-Ray"
    command = 'python(\"lHotkey.hotkeyCmd(\\"%s\\")\")' % cmdName
    mc.nameCommand(cmdName, annotation=ann, c=command)
    mc.hotkey(keyShortcut='X', altModifier=True, ctrlModifier=False, name=cmdName)
    
    # X-Ray Joints
    cmdName = 'xRayJoint'
    ann = "[ Shift + Ctrl + x ] X-Ray Joints"
    command = 'python(\"lHotkey.hotkeyCmd(\\"%s\\")\")' % cmdName
    mc.nameCommand(cmdName, annotation=ann, c=command)
    mc.hotkey(keyShortcut='X', altModifier=False, ctrlModifier=True, name=cmdName)

    # Wireframe On Shaded
    cmdName = 'wireframe'
    ann = "[ Shift + Ctrl + w ] Wireframe on Shaded"
    command = 'python(\"lHotkey.hotkeyCmd(\\"%s\\")\")' % cmdName
    mc.nameCommand(cmdName, annotation=ann, c=command)
    mc.hotkey(keyShortcut='W', altModifier=False, ctrlModifier=True, name=cmdName)

    # Connection Editeor
    cmdName = 'connectionEditeor'
    ann = "[ Ctrl + Alt + q] Connection Editeor"
    command = 'connectWindow(0,"","");'
    mc.nameCommand(cmdName, annotation=ann, c=command)
    mc.hotkey(keyShortcut='q', altModifier=True, ctrlModifier=True, name=cmdName)

    # Hypergraph Window
    cmdName = 'hypergraphWindow'
    ann = "[ Ctrl + Alt + w] Hypergraph Window"
    command = 'tearOffPanel "Hypergraph" "hyperGraphPanel" true;'
    mc.nameCommand(cmdName, annotation=ann, c=command)
    mc.hotkey(keyShortcut='w', altModifier=True, ctrlModifier=True, name=cmdName)

    return True

def hotkeyCmd(cmdName):
    
    withFocus = mc.getPanel(wf=True)
    
    if cmdName == 'showSelect':
        if "outlinerPanel" not in withFocus:
            if mc.modelEditor(withFocus, q=True, vs=True):
                mc.isolateSelect(withFocus, state=False)
            else:
                mc.isolateSelect(withFocus, state=True)
                mc.isolateSelect(withFocus, rs=True)
                mc.isolateSelect(withFocus, addSelected=True)
        
    elif cmdName == 'resetChannelBoxAttribute':
        sels = mc.ls(sl=True)
        for sel in sels:
            con.channelAttrToDefault(sel)
    
    elif cmdName == 'resetTranslate':
        sels = mc.ls(sl=True)
        for sel in sels:
            for attr in ['tx', 'ty', 'tz']:
                try:
                    mc.setAttr('%s.%s' % (sel, attr), 0)
                except:
                    raise
    
    elif cmdName == 'resetRotate':
        sels = mc.ls(sl=True)
        for sel in sels:
            for attr in ['rx', 'ry', 'rz']:
                try:
                    mc.setAttr('%s.%s' % (sel, attr), 0)
                except:
                    raise
    elif cmdName == 'xRayJoint':
        if mc.modelEditor(withFocus, q=True, jointXray=True):
            mc.modelEditor(withFocus, edit=True, jointXray=False)
        else:
            mc.modelEditor(withFocus, edit=True, jointXray=True)
    
    elif cmdName == 'xRay':
        if mel.eval('getPanel -typeOf "%s"' % withFocus) == "modelPanel":
            xrayIO = mc.modelEditor(withFocus, q=True, xray=True)
            if xrayIO:
                mc.modelEditor(withFocus, edit=True, xray=False)
            else:
                mc.modelEditor(withFocus, edit=True, xray=True)
                
    
    elif cmdName == "wireframe":
        if mc.modelEditor(withFocus,q=True,wos=True):
            mc.modelEditor(withFocus,e=True,wos=False)
        else:
            mc.modelEditor(withFocus,e=True,wos=True)
    
    
    
    elif cmdName == 'help':
        print "+ {:-^50}".format(" Hotkey Help ")
        print "|"
        print "+ [ Alt + q ]            Single display selection"
        print "+ [ Ctrl + Alt + q ]     Connection Editeor"
        print "+ [ Ctrl + Alt + w ]     Hypergraph Window"
        print "+ [ Shift + Alt + r ]    Reset the choice of channel box attribute"
        print "+ [ Shift + Alt + w ]    Reset the choice of translate attribute"
        print "+ [ Shift + Alt + e ]    Reset the choice of rotate attribute"
        print "+ [ Shift + Alt + x ]    X-Ray"
        print "+ [ Shift + Ctrl + x ]   X-Ray Joints"
        print "+ [ Shift + Ctrl + w ]   Wireframe on Shaded"
        print "|"
        print "+ {:-^50}".format(" End ")

    return



if __name__ == '__main__':
    pass